<%@ page import="java.security.MessageDigest" %>
<%@ page import="org.w3c.dom.Document" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/4/6
  Time: 17:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!
   private static final String KEY = ":TNF.com";

    public final static String calcMD5(String ss){
        String s = ss == null?"":ss;
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
        try{
            byte[] strTemp = s.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[2*j];
            int k = 0;
            for(int i = 0; i < j; i++ ){
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0  & 0xf];
            }
            return new String(str);
        }catch (Exception e){
            return e.getMessage();
        }
    }
%>
<html>
<head>
    <title>MD5</title>
    <style type="text/css">
        td{
            text-align: right;
        }
        legend{
            font-family: Arial;
            color: aqua;
            font-size: large;
        }
    </style>
</head>
<body>
    <div style="margin:0 auto;width:400px;">
        <fieldset>
            <legend>加密后的信息</legend>
            <div id="info"></div>
            <script type="text/javascript">
                <%
                   String i = request.getParameter("username");
               %>
                <%
                   String j = request.getParameter("password");
               %>
                <%
                    String k = calcMD5(request.getParameter("password"));
                %>
                var i = '<%=i%>';
                var j = '<%=j%>';
                var k = '<%=k%>';
                info.innerHTML = '<table>'+
                        '<tr>'+"<td>username:</td>" + '<td>'+ i +'</td>'+"</tr><tr>" +
                        "<td>password:</td>" + '<td>'+ j +'</td>' + "</tr> <tr>" +
                        "<td>MD5password:</td>"+'<td>'+ k +'</td>'+'</tr>'
                        +'</table>';
            </script>
        </fieldset>
        <fieldset>
            <legend>userInfo</legend>
            <form action="MD5.jsp" method="post">
            <table align="center">
                <tr>
                    <td>username:</td>
                    <td><input id="username" name="username" type="text"/> </td>
                </tr>
                <tr>
                    <td>password:</td>
                    <td><input id="password" name="password" type="password"/></td>
                </tr>
                <tr>
                    <td></td>
                    <td style="text-align: left"><input type="submit" value="提交"/> </td>
                </tr>
            </table>
            </form>
        </fieldset>
    </div>
</body>
</html>
